package org.apache.osgimaker.editors.content;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.MessageFormat;

import org.apache.osgimaker.editors.model.AnalyseReportModel;
import org.apache.osgimaker.editors.model.ClassItem;
import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.bindings.keys.ParseException;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.jface.fieldassist.FieldDecoration;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.IFormPart;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.IPartSelectionListener;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;


public class ClassItemGeneralInfoPart extends SectionPart implements PropertyChangeListener,IPartSelectionListener{

	private AnalyseReportModel model;
	private IManagedForm managedForm;
	
	private Text txtSymbolicName;
	private Text txtVersion;
	private Text txtActivator;
	
	public static final char[] AUTO_ACTIVATION_CLASSNAME = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890._".toCharArray();
	
	public ClassItemGeneralInfoPart(Composite parent, FormToolkit toolkit, int style) {
		super(parent, toolkit, style);
		createSection(getSection(), toolkit);
	}
	
	private void createSection(Section section, FormToolkit toolkit) {
		section.setText("Class Basic Information");

		KeyStroke assistKeyStroke = null;
		try {
			assistKeyStroke = KeyStroke.getInstance("Ctrl+Space");
		} catch (ParseException x) {
			// Ignore
		}
		FieldDecoration contentAssistDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(FieldDecorationRegistry.DEC_CONTENT_PROPOSAL);
		FieldDecoration infoDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration(FieldDecorationRegistry.DEC_INFORMATION);

		Composite composite = toolkit.createComposite(section);
		section.setClient(composite);

		toolkit.createLabel(composite, "Used By:");
		txtSymbolicName = toolkit.createText(composite, "", SWT.BORDER);

		toolkit.createLabel(composite, "Uses Internal:");
		txtVersion = toolkit.createText(composite, "", SWT.BORDER);
		
		toolkit.createLabel(composite, "Uses External:");
		txtActivator = toolkit.createText(composite, "", SWT.BORDER);

		ControlDecoration decorActivator = new ControlDecoration(txtActivator, SWT.LEFT | SWT.TOP, composite);
		decorActivator.setImage(contentAssistDecoration.getImage());
		decorActivator.setDescriptionText(MessageFormat.format("Content Assist is available. Press {0} or start typing to activate", assistKeyStroke.format()));
		decorActivator.setShowOnlyOnFocus(true);
		decorActivator.setShowHover(true);

		// Layout
		GridLayout layout = new GridLayout(2, false);

		composite.setLayout(layout);

		GridData gd;
		gd = new GridData(SWT.FILL, SWT.TOP, true, false);
		gd.horizontalIndent = 5;
		txtSymbolicName.setLayoutData(gd);

		gd = new GridData(SWT.FILL, SWT.TOP, true, false);
		gd.horizontalIndent = 5;
		txtVersion.setLayoutData(gd);

		gd = new GridData(SWT.FILL, SWT.TOP, true, false);
		gd.horizontalIndent = 5;
		txtActivator.setLayoutData(gd);
	
	}
	
	@Override
	public void propertyChange(PropertyChangeEvent arg0) {
		
	}

	@Override
	public void commit(boolean onSave) {
		super.commit(onSave);
		
	}

	@Override
	public void refresh() {
		super.refresh();
	}
	
	@Override
	public void initialize(IManagedForm form) {
		super.initialize(form);
		this.managedForm = form;
		model = (AnalyseReportModel) form.getInput();
	}
	
	@Override
	public void dispose() {
		super.dispose();
		if(this.model != null)
			this.model.removePropertyChangeListener(this);
	}

	@Override
	public void selectionChanged(IFormPart part, ISelection selection) {
		Object[] tmp = ((IStructuredSelection) selection).toArray();
		for(int i=0;i<tmp.length;i++){
			String name = tmp[i].toString();
			ClassItem cycle = model.getClassByName(name);
			txtSymbolicName.setText(cycle==null?"":String.valueOf(cycle.getUsedBy()));
			txtVersion.setText(cycle==null?"":String.valueOf(cycle.getUsesInternal()));
			txtActivator.setText(cycle==null?"":String.valueOf(cycle.getUsesExternal()));
			break;
		}
		
	}
}
